package com.yuyou.tas.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.tas.admin.service.IAuthResourceService;
import com.yuyou.tas.admin.vo.resp.RespAuthRoleResource;
import com.yuyou.tas.util.bean.ReturnResult;

@Api(value="AuthResourceController",description="资源管理接口")
@Validated
@RestController
@RequestMapping(value="/api/authResource")
public class AuthResourceController extends BaseController{

    @Autowired
    private IAuthResourceService authResourceService;

    @ApiOperation(value="角色资源列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "authRoleId", value = "角色id", paramType="query", required = true)})
    @RequestMapping(value="/authRoleResourceList", method = { RequestMethod.POST })
    public ReturnResult<List<RespAuthRoleResource>> authRoleResourceList(@NotNull(message="authRoleId 不能为空") Long authRoleId){

        List<RespAuthRoleResource> allAuthResourceWithChildren = authResourceService.findAllAuthResourceWithChildren(authRoleId);

        return ReturnResult.success(allAuthResourceWithChildren);
    }


    @ApiOperation(value="保存角色资源")
    @ApiImplicitParams({@ApiImplicitParam(name = "authRoleId", value = "角色id", paramType="query", required = true),
                        @ApiImplicitParam(name = "authResourceIdsStr", value = "资源ids 字符串, 逗号隔开", paramType="query", required = true)
    })
    @RequestMapping(value="/saveAuthRoleResourceList", method = { RequestMethod.POST })
    public ReturnResult saveAuthRoleResourceList(@NotNull(message="authRoleId 不能为空") Long authRoleId,
                                                 @NotBlank(message="authResourceIdsStr 不能为空")String authResourceIdsStr){


        List<Long> authResourceIds = Arrays.asList(authResourceIdsStr.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        authResourceService.saveAuthRoleResourceList(authRoleId, authResourceIds);
        return ReturnResult.success("OK");
    }

}
